/**
 * @file   colorRule.cpp
 * @author wolfhead <wolfhead@wolfhead-laptop>
 * @date   Mon Dec  5 20:40:07 2011
 * 
 * @brief  
 * 
 * 
 */
#include "colorRule.h"
#include "colorConfig.h"
#include "colorContain.h"
#include "colorMatch.h"


//////////////////////////////////////////////////////////////////////
// ColorRule
int ColorRule::Create(
    const ColorConfig * pColorConfig,
    const std::string& param, 
    const std::string& color)
{
    _SetColor(pColorConfig, color);
    
    return _Create(param);
}

bool ColorRule::Match(
    const std::string& line)
{
    return _Match(line);
}

const std::string& ColorRule::GetColor()
{
    return color_;
}

int ColorRule::_SetColor(
    const ColorConfig * pColorConfig,
    const std::string& color)
{
    color_ = pColorConfig->GetColor(color);
    return 0;
}

//////////////////////////////////////////////////////////////////////
// ColorRuleFactory
ColorRule* ColorRuleFactory::CreateColorRule(
    const ColorConfig * pColorConfig,
    const std::string& type,
    const std::string& param,
    const std::string& color)
{
    ColorRule* pRule = NULL;
    if (type == "match")
    {
        pRule = new ColorMatch;
    }
    else if (type == "contain")
    {
        pRule = new ColorContain;
    }

    if (pRule)
    {
        if (pRule->Create(pColorConfig, param, color) != 0)
        {
            delete pRule;
            return NULL;
        }
    }

    return pRule;
}
